package com.test41_JDBC应用;

import com.alibaba.fastjson.JSON;
import com.test40_JDBC.util.JDBCUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * 返回树形结构数据
 */
public class YootkDemo_01 {

    public static void main(String[] args) throws Exception {
        Connection conn = JDBCUtil.getConnection();
        Statement stmt = conn.createStatement();
        String sql = "select id, pid, address from region";
        ResultSet rs = stmt.executeQuery(sql);

        // 查询结果放入list
        List<Region> regionList = new ArrayList<>();
        while (rs.next()) {
            Long id = rs.getLong("id");
            Long pid = rs.getLong("pid");
            String address = rs.getString("address");
            Region region = new Region(id, pid, address);
            regionList.add(region);
        }

        // 将list封装为tree
        long startTime = System.currentTimeMillis();
        List<RegionTree> tree = getTree(regionList, 0L);
        long endTime = System.currentTimeMillis();
        System.out.println(JSON.toJSON(tree));
        System.out.println("花费时间：" + (endTime - startTime));

        JDBCUtil.closeConnection(conn);
    }

    /**
     * 返回树形结构数据
     * @param regionList 数据列表
     * @param pid 父id
     * @return
     */
    public static List<RegionTree> getTree(List<Region> regionList, Long pid) {
        if (regionList != null && !regionList.isEmpty()) {
            List<RegionTree> result = new ArrayList<>();
            for (Region region : regionList) {
                if (region.getPid().equals(pid)) {
                    RegionTree regionTree = new RegionTree(region);
                    // 递归获取子列表
                    List<RegionTree> subList = getTree(regionList, regionTree.getId());
                    regionTree.setSubList(subList);
                    result.add(regionTree);
                }
            }
            return result;
        }
        return null;
    }
}

class Region {
    private Long id;
    private Long pid;
    private String address;

    public Region() {
    }

    public Region(Long id, Long pid, String address) {
        this.id = id;
        this.pid = pid;
        this.address = address;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getPid() {
        return pid;
    }

    public void setPid(Long pid) {
        this.pid = pid;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

class RegionTree {
    private Long id;
    private Long pid;
    private String address;
    private List<RegionTree> subList;

    public RegionTree() {
    }

    public RegionTree(Long id, Long pid, String address) {
        this.id = id;
        this.pid = pid;
        this.address = address;
    }

    public RegionTree(Region region) {
        this(region.getId(), region.getPid(), region.getAddress());
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getPid() {
        return pid;
    }

    public void setPid(Long pid) {
        this.pid = pid;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public List<RegionTree> getSubList() {
        return subList;
    }

    public void setSubList(List<RegionTree> subList) {
        this.subList = subList;
    }
}
